<?php

/**
 * 公海列表
 * @Author: wjf
 * @Date: 2022-11-25 19:04:31 
 * */

namespace app\crm\controller\client;

use app\crm\model\client\Ourclients;
use think\Controller;
use think\Db;
use think\facade\Session;

class Highseas extends Controller
{
    /**
     * 主页面 
     * @Author: wjf
     * @Date: 2022-11-26 15:53:32
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 数据，分页
     * @Author: wjf
     * @Date: 2022-11-19 17:19:17
     */
    public function list()
    {

        $page = ($_GET['page'] - 1) * $_GET['limit'];
        $data = Ourclients::limit($page, $_GET['limit'])->where('status', 0)->where('super_id', 0)->where('classify', 0)->select();
        $count = Ourclients::where('status', 0)->where('super_id', 0)->where('classify', 0)->count();
        return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
    }

    /**
     * 领取 
     * @Author: wjf
     * @Date: 2022-12-03 18:44:27
     */
    public function get()
    {
        if ($this->request->isGet() && $this->request->isAjax()) {
            $time = time();
            if (Ourclients::where('id', $_GET['id'])->update(['super_id' => 1, 'status' => 2, 'classify' => 1, 'get_time' => $time])) {
                return json(['code' => 0, 'msg' => '领取成功']);
            }
        }
    }

    /**
     * 批量领取 
     * @Author: wjf
     * @Date: 2022-12-03 18:44:27
     */
    public function getd()
    {
        // 启动事务
        Db::startTrans();
        if ($this->request->isPost() && $this->request->isAjax()) {
            $id = $_POST['id'];
            $time = time();
            $arr = explode(',', $id);
            array_pop($arr);
            $list = [];
            try {
                foreach ($arr as $k => $v) {
                    Ourclients::where('id', $v)->update(['super_id' => 1, 'status' => 2, 'classify' => 1, 'get_time' => $time]);
                }
                // 提交事务
                Db::commit();
                return json(['code' => 0, 'msg' => '领取成功']);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 1, 'msg' => '领取失败']);
            }
        }
    }

    /**
     * 搜索查询
     * @Author: wjf
     * @Date: 2022-12-03 09:19:50
     */
    public function lists()
    {
        $aa = $this->request->get('data');
        $arr = [];
        foreach ($aa as $k => $v) {
            if (!$v) {
                unset($aa[$k]);
            } else {
                $arr[$k] = $v;
            }
        }
        $page = ($_GET['page'] - 1) * $_GET['limit'];
        $Ourclients = new Ourclients();
        $data = $Ourclients->limit($page, $_GET['limit'])->where($arr)->where('super_id', 0)->where('status', 0)->where('classify', 0)->select();
        $count = $Ourclients->where($arr)->where('super_id', 0)->where('status', 0)->where('classify', 0)->count();
        return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
    }
}
